package com.ruoyi.web.controller.server;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysSupervise;
import com.ruoyi.system.service.ISysSuperviseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.service.ISysTeacherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 家教教师Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@RestController
@RequestMapping("/system/teacher")
public class SysTeacherController extends BaseController
{
    @Autowired
    private ISysTeacherService sysTeacherService;

    @Autowired
    private ISysSuperviseService superviseService ;

    /**
     * 查询家教教师列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysTeacher sysTeacher)
    {
        startPage();
        List<SysTeacher> list = sysTeacherService.selectSysTeacherList(sysTeacher);
        return getDataTable(list);
    }

    @PostMapping("/hireTeacher")
    public AjaxResult hireTeacher(@RequestBody SysSupervise supervise) {
        if (ObjectUtils.isEmpty(supervise)) {
            throw new ServiceException("聘请信息不能为空!") ;
        }
        if (ObjectUtils.isEmpty(supervise.getTeacherId())) {
            throw new ServiceException("教师编号不能为空!") ;
        }
        if (ObjectUtils.isEmpty(supervise.getTeacherId())) {
            throw new ServiceException("学生编号不能为空!") ;
        }
        LambdaQueryWrapper<SysSupervise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysSupervise::getTeacherId ,supervise.getTeacherId()) ;
        wrapper.eq(SysSupervise::getStudentId ,supervise.getStudentId()) ;
        SysSupervise one = superviseService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(one)) {
            throw new ServiceException("不能重复聘请同一位教师!") ;
        }
        superviseService.save(supervise);
        return AjaxResult.success() ;
    }

    /**
     * 导出家教教师列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTeacher sysTeacher)
    {
        List<SysTeacher> list = sysTeacherService.selectSysTeacherList(sysTeacher);
        ExcelUtil<SysTeacher> util = new ExcelUtil<SysTeacher>(SysTeacher.class);
        util.exportExcel(response, list, "家教教师数据");
    }

    /**
     * 获取家教教师详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTeacherService.selectSysTeacherById(id));
    }

    /**
     * 新增家教教师
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysTeacher sysTeacher)
    {
        return toAjax(sysTeacherService.insertSysTeacher(sysTeacher));
    }

    /**
     * 修改家教教师
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SysTeacher sysTeacher)
    {
        return toAjax(sysTeacherService.updateSysTeacher(sysTeacher));
    }

    /**
     * 删除家教教师
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysTeacherService.deleteSysTeacherByIds(ids));
    }

    @PostMapping("/changeTeacherApplicationStatus")
    public AjaxResult changeTeacherApplicationStatus(@RequestBody SysTeacher teacher) {
        if (ObjectUtils.isEmpty(teacher)) {
            throw new ServiceException("教师信息不能为空!") ;
        }
        if (ObjectUtils.isEmpty(teacher.getId())) {
            throw new ServiceException("教师编号不能为空!") ;
        }
        if (StringUtils.isBlank(teacher.getStatus())) {
            throw new ServiceException("教师状态不能为空!") ;
        }
        boolean update = sysTeacherService.updateById(teacher);
        if (update) {
            return AjaxResult.success(update) ;
        }else {
            return AjaxResult.error() ;
        }
    }
}
